import request, { headers } from '@/utils/request'
import qs from 'qs'
const UUID = require('uuid')

export default {
  clientInfo(connName){return request.get('/db/metadata/clientInfo',{params: {connName}})},
  // 元数据接口
  connections() { return request.get('/security/connect/database/connectNames') },
  catalogs(connName) { return request.get('/db/metadata/catalogs', { params: { connName }}) },
  schemas(connName){return request.get('/db/metadata/schemas',{params:{connName}})},
  catalogSchemas(connName,catalog){return request.get('/db/metadata/filterSchemas',{params: {connName,catalog}})},
  indices(connName,actualTableName){
    const {namespace, tableName} = actualTableName;
    return request.get('/db/metadata/indices',{params: {connName:connName,'namespace.catalog':namespace.catalog,'namespace.schema':namespace.schema,tableName:tableName}})
  },
  primaryKeys(connName,actualTableName){
    const {namespace, tableName} = actualTableName;
    return request.get('/db/metadata/primaryKeys',{params: {connName:connName,'namespace.catalog':namespace.catalog,'namespace.schema':namespace.schema,tableName:tableName}})
  },
  tables(connName, catalog,schema) { return request.get('/db/metadata/tables', { params: { connName, catalog, schema }}) },
  refreshTable(connName,actualTableName){
    const {namespace, tableName} = actualTableName;
    return request.get('/db/metadata/refreshTable',{params: {connName:connName,'namespace.catalog':namespace.catalog,'namespace.schema':namespace.schema,tableName:tableName}})
  },
  searchTables(connName, catalog, schemas,searchSchema, keyword) { return request.get('/db/metadata/searchTables', { params: { connName, catalog, ...{ schemas: schemas.join(',') },searchSchema, keyword }}) },
  compareMeta(compareParam){return request.post('/db/metadata/compare',compareParam,headers.json)},
  compareMetaChangeSqls(compareParam){return request.post('/db/metadata/compare/changeSqls',compareParam,headers.json)},

  // 数据查询
  executeQuery(connName,namespace, sql) { return request.post('/db/data/executeQuery', { connName: connName,namespace, sqls: [sql] }) },
  groups(connName, namespace) { return request.get('/db/data/config/groups', { params: { connName, ...namespace }}) },
  dataIds(connName, namespace, groupId) { return request.get('/db/data/config/dataIds', { params: { connName, ...namespace, groupId }}) },
  content(connName, namespace, groupId, dataId) { return request.get('/db/data/config/content', { params: { connName, ...namespace, groupId, dataId }}) },
  emptyTable(connName, actualTableName) {
    const {namespace, tableName} = actualTableName;
    return request.post('/db/data/emptyTable', qs.stringify({ connName, 'namespace.catalog':namespace.catalog, 'namespace.schema':namespace.schema,tableName: tableName }))
  },
  dataChangeSqls(dataChangeParam){return request.post('/db/data/dataChangeSqls',dataChangeParam,headers.json)},
  loadRandomMethods(){return request.get('/db/data/loadRandomMethods')},
  tableRandomData(connName, actualTableName,columnMappers,size){
    let pushData = {connName,actualTableName,columnMappers,size}
    return request.post('/db/data/singleTableRandomData',pushData,headers.json)
  },
  checkDirtyData(connName,namespace){return request.get('/db/data/checkDirtyData',{params:{connName,...namespace}})},

  // 代码生成相关接口
  renameStrategies() { return request.get('/db/code/renameStrategies') },
  buildJavaBean(params) { return request.post('/db/code/build/javaBean', params, headers.json) },
  buildMapper(params) { return request.post('/db/code/build/mapper', params, headers.json) },
  templates() { return request.get('/db/code/templates') },
  uploadTemplate(formData) { return request.post('/db/code/template/upload', formData) },
  templateContent(templateName) { return request.get(`/db/code/${templateName}/content`) },
  // dropTemplate(templateName){ return request.post('/db/code/template/drop',qs.stringify({templateName}))},
  override(params) { return request.post('/db/code/override', params, headers.json) },
  codeSchemas() { return request.get('/db/code/schemas') },
  codeSchemaTemplates(schema) { return request.get(`/db/code/${schema}/templates`) },
  codePreview(params) { return request.post('/db/code/template/code/preview', params) },
  codeGenerate(params) { return request.post('/db/code/template/code/generator', params) },

  // 表标记,表关系 元数据扩展
  tags() { return request.get('/db/metadata/extend/mark/tags') },
  markTag(connName,params) { return request.post(`/db/metadata/extend/mark/config/${connName}/tableMark`, params, headers.json) },
  tableTags(connName, actualTableName) {
    const {namespace, tableName} = actualTableName;
    return request.get('/db/metadata/extend/mark/tableTags', { params: { connName:connName,'namespace.catalog':namespace.catalog,'namespace.schema':namespace.schema,tableName:tableName}})
  },
  tagTables(connName, catalog, schema, tag) { return request.get('/db/metadata/extend/mark/tagTables', { params: { connName, catalog, schema, tag }}) },
  configRelation(params) { return request.post('/db/metadata/extend/relation/config', params, headers.json) },
  configRelationFromSql(params) { return request.post('/db/metadata/extend/relation/config/fromSql', params, headers.json) },
  parentRelations(connName, table) { return request.get('/db/metadata/extend/relation/parents', { params: { connName, ...table }}) },
  childRelations(connName, table) { return request.get('/db/metadata/extend/relation/childs', { params: { connName, ...table }}) },
  relations(connName, namespace){return request.get('/db/metadata/extend/relation/list',{params: {connName,...namespace}})},
  hierarchy(connName, actualTableName) {
    const {namespace, tableName} = actualTableName;
    return request.get('/db/metadata/extend/relation/hierarchy', { params: { connName:connName,'namespace.catalog':namespace.catalog,'namespace.schema':namespace.schema,tableName:tableName }})
  },
  superTypes(connName, actualTableName) {
    const {namespace, tableName} = actualTableName;
    return request.get('/db/metadata/extend/relation/superTypes', {params: { connName:connName,'namespace.catalog':namespace.catalog,'namespace.schema':namespace.schema,tableName:tableName }})
  },

  // 表数据导出相关
  exportPreview(connName,namespace, sql) {
    // let uuid = uuid.v1();
    // console.log(UUID,'UUID');
    const uuid = UUID.v1()
    return request.post('/db/data/exportPreview', { connName: connName, namespace: namespace, sqls: [sql], traceId: uuid }, headers.json)
  },
  exportData(connName,namespace, sql) {
    // let uuid = uuid.v1()
    const uuid = UUID.v1()
    return request.post('/db/data/exportData', { connName: connName, namespace: namespace, sqls: [sql], traceId: uuid }, headers.json)
  },

  // 导出文档相关
  exportDoc(connName, catalog, schemas, searchSchema, keyword){return request.get('/db/doc/export',{ params: { connName, catalog,searchSchema, ...{ schemas: schemas.join(',') }, keyword }})},
  templateExamples(){return request.get('/db/code/template/examples')},
}
